<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\NodeService;
use app\common\facade\Arr;
use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\services\JobService;
use app\common\services\PublishJobService;
use app\common\tools\ExportExcel;


/**
 * 财务报表
 * Class Index
 * @package app\admin\controller
 */
class PaltChargeStat extends Controller
{
    public $table = 'UserCharge';

    /**
     * 财务统计
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {

        $this->title="财务统计";

        //充值总额
        $result['total_money']=DB::name($this->table)
            ->where('status',1)
            ->sum("money");
        //充值中笔数
        $result['total_pay_num']=DB::name($this->table)
            ->where('status',1)
            ->count('id');
        $this->assign('result',$result);

        $start_time= $this->request->param('start_time');
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }

        }else{
            $today=date("Y-m-d");
            $start=strtotime($today." 00:00:00");
            $end=strtotime($today. " 23:59:59");
        }
        $st[]=['uc.status','EQ',1];
        $st[]=['uc.finished_time','between time',[$start, $end]];
        $query = $this->_query($this->table)
                ->alias('uc')
                ->join("User u",'uc.user_id=u.id')
                ->where($st)
                ->field("uc.*,u.name")
                ->like('uc.finished_time')
                ->equal('uc.finished_time');
        $query->order('uc.id desc')->page(); 
       
    }

   

    /**
     * 财务日报表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function statdata(){

        $this->title="财务日报表";

       
        $start_time= $this->request->param('start_time');
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }
            $data[]=['for_time','between time',[$start, $end]];
        }
        $data[]=['type','EQ',1];

        $query = $this->_query("platform_charge_stat")
                ->where($data)
                ->like('id')
                ->equal('id,for_time');
        $query->order('id desc')->page(); 
    }

       /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _statdata_page_filter(&$data)
    {
        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月d日",$vo['for_time']);

        }
    }


    //每个代理商数据统计
    public function datelist(){
        
        $id =$this->request->param('id');
        $result=DB::name("platform_charge_stat")->where('id',$id)->find();
        $this->assign('result',$result);

        $data[]=['acs.type','EQ',1];
        $data[]=['acs.for_time_start',"EQ",$result['for_time_start']];
        $data[]=['acs.for_time_end',"EQ",$result['for_time_end']];
       
        $query = $this->_query("agency_charge_stat")
                ->alias('acs')
                ->join("system_agent_user sau",'acs.aid=sau.id')
                ->where($data)
                ->field("acs.*,sau.username")
                ->like('acs.for_time')
                ->equal('acs.for_time');
        $query->order('acs.id desc')->page(); 


    }


       /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _datelist_page_filter(&$data)
    {
        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月d日",$vo['for_time']);

        }
    }
     

    //财务日报表详情
    public function detail(){

        $id =$this->request->param('id');
        $result=DB::name("agency_charge_stat")->where('id',$id)->find();
        $this->assign('result',$result);

        $st[]=['u.provinceid','EQ',$result['provinces_id']];
        $st[]=['u.cityid','EQ',$result['city_id']];
        $st[]=['uc.status','EQ',1];
        $st[]=['uc.finished_time','between time',[$result['for_time_start'], $result['for_time_end']]];
        $query = $this->_query($this->table)
                ->alias('uc')
                ->join("User u",'uc.user_id=u.id')
                ->where($st)
                ->field("uc.*,u.name")
                ->like('uc.finished_time')
                ->equal('uc.finished_time');
        $query->order('uc.id desc')->page(); 

    }


    /**
     * 财务月报表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function statmonth(){

        $this->title="财务月报表";

        $start_time= $this->request->param('start_time');
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }
            $data[]=['for_time','between time',[$start, $end]];

        }
        $data[]=['type','EQ',2];
        $query = $this->_query("platform_charge_stat")
                ->where($data)
                ->like('id')
                ->equal('id,for_time');
        $query->order('id desc')->page(); 
    }


        /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _statmonth_page_filter(&$data)
    {

        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月",$vo['for_time_end']);

        }
    }

     //每个代理商每月数据统计
    public function monthlist(){
        
        $id =$this->request->param('id');
        $result=DB::name("platform_charge_stat")->where('id',$id)->find();
        $this->assign('result',$result);

        $data[]=['acs.type','EQ',2];
        $data[]=['acs.for_time_start',"EQ",$result['for_time_start']];
        $data[]=['acs.for_time_end',"EQ",$result['for_time_end']];
       
        $query = $this->_query("agency_charge_stat")
                ->alias('acs')
                ->join("system_agent_user sau",'acs.aid=sau.id')
                ->where($data)
                ->field("acs.*,sau.username")
                ->like('acs.for_time')
                ->equal('acs.for_time');
        $query->order('acs.id desc')->page(); 


    }

       /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _monthlist_page_filter(&$data)
    {
        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月",$vo['for_time']);

        }
    }


    //财务月报表详情
    public function mdetail(){

        $id =$this->request->param('id');
        $result=DB::name("agency_charge_stat")->where('id',$id)->find();
        $this->assign('result',$result);

        $st[]=['u.provinceid','EQ',$result['provinces_id']];
        $st[]=['u.cityid','EQ',$result['city_id']];
        $st[]=['uc.status','EQ',1];
        $st[]=['uc.finished_time','between time',[$result['for_time_start'], $result['for_time_end']]];
        $query = $this->_query($this->table)
                ->alias('uc')
                ->join("User u",'uc.user_id=u.id')
                ->where($st)
                ->field("uc.*,u.name")
                ->like('uc.finished_time')
                ->equal('uc.finished_time');
        $query->order('uc.id desc')->page(); 

    }

    




  

}
